#include "Button.h"

#include "../_Luly.h"
#include "_UI.h"
Button::Button(std::string uiObjectId, std::string text, int x, int y)
    : UIObject(uiObjectId, x, y) {
  textView = new TextView(uiObjectId + "_textView_", text);
  defualtNinePatch = new NinePatch(_UI::getInstance().button_default_ninePatch);
  hoveringNinePatch =
      new NinePatch(_UI::getInstance().button_hovering_ninePatch);
  clickedNinePatch = new NinePatch(_UI::getInstance().button_clicked_ninePatch);
  currentNinePatch = defualtNinePatch;
  layout = new Layout(uiObjectId + "_layout_", Layout::Arrangement::VERTICAL,
                      Layout::Alignment::CENTER);
  layout->addChild(textView);
  addChild(layout);
}

void Button::init() { UIObject::init(); }

void Button::event() {
  SDL_Event& current_event = _Luly::getInstance().current_event;
  int mx, my;
#ifdef _ANDROID_PLATFORM__
  mx = current_event.tfinger.x * _Luly::getInstance().windowWidth;
  my = current_event.tfinger.y * _Luly::getInstance().windowHeight;
  switch (current_event.tfinger.type) {
    case SDL_FINGERMOTION: {
      if (mx > x && mx < x + width && my > y && my < y + height) {
        if (fingerid == nullptr) {
          fingerid = (void*)current_event.tfinger.fingerId;
        }
        if (!isButtonDown) {
          if (couldCancel) {
            onHovering_callback();
            currentNinePatch = hoveringNinePatch;
          } else if (fingerid == (void*)current_event.tfinger.fingerId) {
            //如果还是原来的那个手指
            onButtonDown_callback();
            //绘制
            currentNinePatch = clickedNinePatch;
            isButtonDown = true;
          }
        } else {
          currentNinePatch = clickedNinePatch;
        }
      } else {
        if (fingerid == (void*)current_event.tfinger.fingerId) {
          if (isButtonDown) {  // button->setCouldCancel(false);
            if (couldCancel) {
              currentNinePatch = clickedNinePatch;
            } else {
              currentNinePatch = defualtNinePatch;
              isButtonDown = false;
              onClicked_callback();
              fingerid = nullptr;
            }
          } else {
            currentNinePatch = defualtNinePatch;
            fingerid = nullptr;
          }
        }
      }
      break;
    }
    case SDL_FINGERDOWN: {
      if (mx > x && mx < x + width && my > y && my < y + height) {
        if (fingerid == nullptr) {
          fingerid = (void*)current_event.tfinger.fingerId;
        }
        isButtonDown = true;  //状态设为按下
        currentNinePatch = clickedNinePatch;
        currentNinePatch->drawContent(x, y, contentWidth, contentHeight);
        onButtonDown_callback();  //只会在按下那一瞬间执行
      }
      break;
    }

    case SDL_FINGERUP: {
      if (fingerid == (void*)current_event.tfinger.fingerId) {
        if (isButtonDown) {
          //需要按下，抬起都在按键内才算是点击
          if (mx > x && mx < x + width && my > y && my < y + height) {
            onClicked_callback();
          }
          //按下在按键内，抬起不需要在按键内，是抬起
          onButtonUp_callback();
          isButtonDown = false;
        }
        fingerid = nullptr;
        if (currentNinePatch == hoveringNinePatch) {
          currentNinePatch = defualtNinePatch;
        }
      }

      break;
    }
  }

#endif

#ifndef _ANDROID_PLATFORM__
  if (current_event.type == SDL_MOUSEMOTION) {
    mx = current_event.button.x;
    my = current_event.button.y;
  }
  SDL_GetMouseState(&mx, &my);
  mx = int(mx / _Luly::getInstance().scale);
  my = int(my / _Luly::getInstance().scale);

  switch (current_event.type) {
    case SDL_MOUSEMOTION: {
      if (mx > x && mx < x + width && my > y && my < y + height) {
        if (!isButtonDown) {
          onHovering_callback();
          currentNinePatch = hoveringNinePatch;
        } else {
          currentNinePatch = clickedNinePatch;
        }
      } else {
        if (isButtonDown) {
          if (couldCancel) {
            currentNinePatch = clickedNinePatch;
          } else {
            currentNinePatch = defualtNinePatch;
            isButtonDown = false;
            onClicked_callback();
          }
        } else {
          currentNinePatch = defualtNinePatch;
        }
      }
      break;
    }
    case SDL_MOUSEBUTTONDOWN: {
      if (mx > x && mx < x + width && my > y && my < y + height) {
        isButtonDown = true;  //状态设为已经按下
        currentNinePatch = clickedNinePatch;
        onButtonDown_callback();  //只会在按下那一瞬间执行
      }
      break;
    }
    case SDL_MOUSEBUTTONUP: {
      if (isButtonDown) {
        //需要按下，抬起都在按键内才算是点击
        if (mx > x && mx < x + width && my > y && my < y + height) {
          onClicked_callback();
        }
        //按下在按键内，抬起不需要在按键内，是抬起
        onButtonUp_callback();
        isButtonDown = false;
        // draw();  //及时更新变化
      }
      // currentNinePatch = defualtNinePatch;
      break;
    }
  }

#endif

  if (isButtonDown == true) {
    currentNinePatch = clickedNinePatch;
  } else if (currentNinePatch != hoveringNinePatch) {
    currentNinePatch = defualtNinePatch;
  }
}

void Button::update() { UIObject::update(); }

void Button::draw() {
  // if (layout->getWidthMode() == WHMode::FIT) {
  //   width = layout->getWidth() + currentNinePatch->getContentToLeft() +
  //           currentNinePatch->getContentToRight();
  //   contentWidth = layout->getWidth();
  // } else {
  //   contentWidth = width - currentNinePatch->getContentToLeft() -
  //                  currentNinePatch->getContentToRight();
  // }

  // if (layout->getHeightMode() == WHMode::FIT) {
  //   height = layout->getHeight() + currentNinePatch->getContentToTop() +
  //            currentNinePatch->getContentToBottom();
  //   contentHeight = layout->getHeight();
  // } else {
  //   contentHeight = height - currentNinePatch->getContentToTop() -
  //                   currentNinePatch->getContentToBottom();
  // }

  // contentX = currentNinePatch->getContentToLeft();
  // contentY = currentNinePatch->getContentToTop();
  // layout->setRelativeXY(contentX, contentY);
  // layout->setWidth(contentWidth);
  // layout->setHeight(contentHeight);

  // currentNinePatch->drawContent(x, y, contentWidth, contentHeight);

  // UIObject::draw();
  drawArea(Rect{contentX, contentY, contentWidth, contentHeight});
}

void Button::drawArea(const Rect& area) {
  if (currentNinePatch == clickedNinePatch) {  //当按钮按住时
    onButtonHolding_callback();
  }
  if (layout->getWidthMode() == WHMode::FIT) {
    width = layout->getWidth() + currentNinePatch->getContentToLeft() +
            currentNinePatch->getContentToRight();
    contentWidth = layout->getWidth();
  } else {
    contentWidth = width - currentNinePatch->getContentToLeft() -
                   currentNinePatch->getContentToRight();
  }

  if (layout->getHeightMode() == WHMode::FIT) {
    height = layout->getHeight() + currentNinePatch->getContentToTop() +
             currentNinePatch->getContentToBottom();
    contentHeight = layout->getHeight();
  } else {
    contentHeight = height - currentNinePatch->getContentToTop() -
                    currentNinePatch->getContentToBottom();
  }

  contentX = currentNinePatch->getContentToLeft();
  contentY = currentNinePatch->getContentToTop();
  layout->setRelativeXY(contentX, contentY);
  layout->setWidth(contentWidth);
  layout->setHeight(contentHeight);

  currentNinePatch->drawContentArea(area, x, y, contentWidth, contentHeight);

  UIObject::drawArea(area);
}

Button::~Button() {
  delete defualtNinePatch;
  delete hoveringNinePatch;
  delete clickedNinePatch;
}
